﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.MarketPlace;
using Jalal.Library.DataStructure;
using BCGC.BLL.BusinessObject.UserAccount;
using BCGC.BLL.MailManager;
using Jalal.Library.Web;

public partial class marketplace : PublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }

    }

    private void LoadData()
    {
        ReadWriteCustomCollection<Market> markets = Market.GetAllMarketListing(string.Empty, null, null, null, 1, 50);
        if (markets != null)
        {
            repMarketPlace.DataSource = markets;
            repMarketPlace.DataBind();
        }
    }
    protected void btnSendMessage_Command(object sender, CommandEventArgs e)
    {
        if (SessionManager.IsUserLoggedIn)
        {
            string userID = txtUserID.Value;
            UserInfo receiver = UserInfo.GetUserInfoByUserID(Convert.ToInt32(userID));
            UserInfo senderUser = UserInfo.GetUserInfoByUserID(SessionManager.LoggedUser.Id);

            PrivateMessage message = new PrivateMessage();
            message.Content = Server.HtmlEncode(txtBody.Text.Trim());
            message.IsMarketPlaceMessage = true;
            message.IsActive = true;
            message.IsRead = false;
            message.ReceiverEmailID = receiver.Email;
            message.ReceiverID = receiver.UserId;
            message.ReceiverName = receiver.Username;
            message.SendDate = DateTime.Now;
            message.SenderID = senderUser.UserId;
            message.SenderName = senderUser.Username;
            message.Subject = Server.HtmlEncode(txtSubject.Text.Trim());
            message.IsShowInBox = true;
            message.IsShowOutBox = true;
            message.ReplyToMessageID = 0;
            message.IsReply = false;
            if (message.Save())
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "_SuccessMessage", "alert('Message Sent to " + senderUser.Username + "');", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "_FailMessage", "alert('Failed to Send Message.Try again Later.');", true);
            }
        }
        else
        {
            Response.Redirect(CustomSiteMap.Login);
        }


    }
    protected void imbStartTopic_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(CustomSiteMap.SubmitToMarket);  
    }
}
